<template>
	<view class="content" style="width: 100vw;height: 100vh;">
		<view class="login-bg">
			<view class="login-card">
				<view class="login-head">用户登录</view>
				<view class="viewbox">


					<view class="login-input login-margin-b">
						<input type="text" v-model="username" placeholder="请输入您的账号" />
					</view>
					<view class="login-input">
						<input type="password" v-model="password" placeholder="请输入您的密码" />
					</view>
					<view class="login-function">
						<label class="login-register radio" @click="changeCheck">
							<radio class="margin-right-sm" value="" :checked="ischeck" /><text>已阅读并同意用户服务协议</text>
						</label>

					</view>
					<view class="login-btn">
						<button class="cu-btn block bg-blue margin-tb-sm lg" @click="go_login">立即登录</button>
					</view>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var api = require('../../api/api.js');
	export default {
		data() {
			return {
				userid: '',
				username: '',
				password: '',
				balance: '',
				type: [],
				token: '',

				ischeck: false, //未确认协议

			}
		},
		onLoad() {

		},
		methods: {
			// 协议确认radio
			changeCheck() {
				this.ischeck = !this.ischeck
			},
			go_login() {

				console.log(this.username)
				// 如果选中确认
				if (this.ischeck == true) {
					//判断并保存登录状态
					uni.request({
						url: api.Login,
						method: 'post',
						data: {
							username: this.username,
							password: this.password
						},
						success: res => {
							// 此处先规划骨架
							if (res.data.errmsg == "成功") {
								console.log(res.data.data);
								//alert(res.data.msg);
								let userdata = {
									"username": res.data.data.userInfo.nickName,
									"token": res.data.data.token,
									"userid": res.data.data.id,
									"type": res.data.data.typeList,

								}
								try {
									uni.setStorageSync('userid', userdata.userid);
									uni.setStorageSync('username', userdata.username);
									// uni.setStorageSync('balance', userdata.balance); 
									uni.setStorageSync('token', userdata.token);
									uni.setStorageSync('type', userdata.type);

								} catch (e) {
									// error
								}
								uni.showToast({
									icon: 'success',
									position: 'bottom',
									title: res.data.msg
								});

								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/allIndex/allIndex',
										// url: '/pages/index/index',
									});
								}, 300);
								uni.request({
									url: api.Price,
									method: 'post',
									data: {
										username: uni.getStorageSync('username'),
									},
									success: res => {
										console.log(res.data.data.balance)
										let userdata = {
											"balance": res.data.data.balance,
										}
										try {
											uni.setStorageSync('balance', userdata.balance);

										} catch (e) {
											// error
										}
									}
								})
							} else {
								uni.showToast({
									image: '../../static/x.png',
									icon: 'roundclose',
									position: 'bottom',
									title: res.data.errmsg
								});
							}
							//this.article =  res.data.data;
						},
						fail: () => {}
					});
				}

			
			if(this.ischeck==false){
				uni.showToast({
					title:"请先勾选同意！",
					icon:"none"
				})
			}
			
			}
			// 如果没有选中确认

		}

	}
</script>

<style>
	.content {
		background: url("../../static/bg.jpg");
		background-size: 100% 100%;
		height: 100%;
		position: fixed;
		width: 100%
	}

	.login-btn button {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #3A8FFF, #3A6DFF);
	}

	.login-btn {
		padding: 10upx 20upx;

	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: right;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: left;
		font-size: 26upx;

	}

	.login-input input {
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		background: none;
		border-bottom: 1px solid #999;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;

	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		border-top-left-radius: 20upx;
		border-top-right-radius: 20upx;
		padding: 25upx;
		background: #0063FF;
		color: #fff;
	}

	.login-card {
		background: #fff;
		border-radius: 20upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.2);
		position: relative;
		margin-top: 300upx;
	}

	.login-bg {
		padding: 25upx;
	}

	.viewbox {
		padding: 20px 30px;
	}
</style>
